﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace CPA
{
    class character
    {
        Texture2D[] sprites;
        Texture2D textbox;
        SpriteBatch spriteBatch;
        string name;
        string[] saying;
        public int x, y;
        SpriteFont font;
        public int charW = 25, charH = 56;

        public int hp=100;
        public int lives = 5;
        int frames,frames2 = 0;

        UInt16 state;

        const UInt16 W_LEFT  =   1;
        const UInt16 W_RIGHT =   2;
        const UInt16 W_F_1   =   4; // walk frames
        const UInt16 W_F_2   =   8;
        const UInt16 W_F_3   =  12;
        const UInt16 W_F_4   =  16;
        const UInt16 TALK_1  =  32;
        const UInt16 TALK_2  =  64;
        const UInt16 ATTACK  = 128;
        const UInt16 JUMP    = 256;

        public character(Texture2D[] spriteset, SpriteBatch spriteBatch, string name, Texture2D textbox, int x, int y, SpriteFont font)
        {
            this.sprites = spriteset;
            this.spriteBatch = spriteBatch;
            this.name = name;
            this.textbox = textbox;
            this.x = x;
            this.y = y;
            this.font = font;
            this.state = (W_LEFT | W_F_1);
        }

        public void walk_left()
        {
            frames2++;
            // Start the animation for walking left, while preserving any talk animations in the foreground if needed
            if ((this.state & TALK_1) == TALK_1)
            {
                this.state = (W_LEFT | TALK_1);
            }
            else if ((this.state & TALK_2) == TALK_2)
            {
                this.state = (W_LEFT | TALK_2);
            }
            else
            {
                this.state = (W_LEFT);
            }
            if (frames2 >= 0 && frames2 < 10)
            {
                this.state = (ushort)(this.state | W_F_1);
            }
            else if (frames2 >= 10 && frames2 < 20)
            {
                this.state = (ushort)(this.state | W_F_2);
            }
            else if (frames2 >= 20 && frames2 < 30)
            {
                this.state = (ushort)(this.state | W_F_3);
            }
            else if (frames2 >= 30 && frames2 < 39)
            {
                this.state = (ushort)(this.state | W_F_4);
            }
            else if (frames2 >= 40)
            {
                this.state = (ushort)(this.state | W_F_1);
                this.frames2 = 0;
            }
        }

        public void walk_right()
        {
            frames2++;
            // Start the animation for walking right, while preserving any talk animations in the foreground if needed
            if ((this.state & TALK_1) == TALK_1)
            {
                this.state = (W_RIGHT | TALK_1);
            }
            else if ((this.state & TALK_2) == TALK_2)
            {
                this.state = (W_RIGHT | TALK_2);
            }
            else
            {
                this.state = (W_RIGHT);
            }
            if (frames2 >= 0 && frames2 < 10)
            {
                this.state = (ushort)(this.state | W_F_1);
            }
            else if (frames2 >= 10 && frames2 < 20)
            {
                this.state = (ushort)(this.state | W_F_2);
            }
            else if (frames2 >= 20 && frames2 < 30)
            {
                this.state = (ushort)(this.state | W_F_3);
            }
            else if (frames2 >= 30 && frames2 < 39)
            {
                this.state = (ushort)(this.state | W_F_4);
            }
            else if (frames2 >= 40)
            {
                this.state = (ushort)(this.state | W_F_1);
                this.frames2 = 0;
            }
        }

        public void jump()
        {
            this.state = (ushort)(this.state | JUMP);
        }

        public void melee() 
        {
            this.state = (ushort)(this.state | ATTACK);
        }

        public void talk()
        {
            // Without a given set of text, end the talking animation.
            this.state = (ushort)(this.state & (~(TALK_1 | TALK_2))); // Tell it to shut up. ^.~
            this.saying = new string[4];
        }

        public void talk(string[] text) 
        {
            // Begin the talking animation, showing speech box and talker in foreground
            this.state = (ushort)((this.state & ~TALK_2) | TALK_1);
            this.saying = text;
        }

        public void stand()
        {
            // Stop walking, but preserve speech and direction
            if ((this.state & W_LEFT) == W_LEFT)
            {
                this.state = (TALK_1 | W_LEFT);
            }
            else if ((this.state & W_RIGHT) == W_RIGHT)
            {
                this.state = (TALK_1 | W_RIGHT);
            }
            else if ((this.state & TALK_1 & W_LEFT) == (W_LEFT & TALK_1))
            {
                this.state = (TALK_1 | W_F_1 | W_LEFT);
            }
            else if ((this.state & TALK_1 & W_RIGHT) == (W_RIGHT & TALK_1))
            {
                this.state = (TALK_1 | W_F_1 | W_RIGHT);
            }
            else if ((this.state & TALK_2 & W_LEFT) == (W_LEFT & TALK_2))
            {
                this.state = (TALK_2 | W_F_1 | W_LEFT);
            }
            else if ((this.state & TALK_2 & W_RIGHT) == (W_RIGHT & TALK_2))
            {
                this.state = (TALK_2 | W_F_1 | W_RIGHT);
            }
        }

        public void update()
        {
            frames++;
        }

        public void draw()
        {
            // Ingame Sprite Display
            if ((this.state & W_LEFT) == W_LEFT)
            {
                if ((this.state & W_F_1) == W_F_1)
                    spriteBatch.Draw(this.sprites[2], new Rectangle(this.x, this.y, charW, charH), Color.White);
                else if ((this.state & W_F_2) == W_F_2)
                    spriteBatch.Draw(this.sprites[3], new Rectangle(this.x, this.y, charW, charH), Color.White);
                else if ((this.state & W_F_3) == W_F_3)
                    spriteBatch.Draw(this.sprites[4], new Rectangle(this.x, this.y, charW, charH), Color.White);
                else if ((this.state & W_F_4) == W_F_4)
                    spriteBatch.Draw(this.sprites[5], new Rectangle(this.x, this.y, charW, charH), Color.White);
                else if ((this.state & JUMP) == JUMP)
                    spriteBatch.Draw(this.sprites[15], new Rectangle(this.x, this.y, charW, charH), Color.White);

            }
            else if ((this.state & W_RIGHT) == W_RIGHT)
            {
                if ((this.state & W_F_1) == W_F_1)
                {
                    spriteBatch.Draw(this.sprites[6], new Rectangle(this.x, this.y, charW, charH), Color.White);
                    Console.WriteLine(1);
                }
                else if ((this.state & W_F_2) == W_F_2)
                {
                    spriteBatch.Draw(this.sprites[7], new Rectangle(this.x, this.y, charW, charH), Color.White);
                    Console.WriteLine(2);
                }
                else if ((this.state & W_F_3) == W_F_3)
                {
                    spriteBatch.Draw(this.sprites[8], new Rectangle(this.x, this.y, charW, charH), Color.White);
                    Console.WriteLine(3);
                }
                else if ((this.state & W_F_4) == W_F_4)
                {
                    spriteBatch.Draw(this.sprites[9], new Rectangle(this.x, this.y, charW, charH), Color.White);
                    Console.WriteLine(4);
                }
                else if ((this.state & JUMP) == JUMP)
                    spriteBatch.Draw(this.sprites[16], new Rectangle(this.x, this.y, charW, charH), Color.White);
            }

            // Display sword/weapon for attack if needed
            if ((this.state & ATTACK) == ATTACK)
            {
                if ((this.state & W_LEFT) == W_LEFT)
                    spriteBatch.Draw(this.sprites[10], new Rectangle(this.x, this.y, charW, charH), Color.White);
                else if ((this.state & W_RIGHT) == W_RIGHT)
                    spriteBatch.Draw(this.sprites[11], new Rectangle(this.x, this.y, charW, charH), Color.White);
            }

            // Show textbox, talker, and speech in foreground
            if ((this.state & TALK_1) == TALK_1)
            {
                spriteBatch.Draw(this.sprites[0], new Rectangle(556, 10, 229, 470), Color.White);
                spriteBatch.Draw(this.textbox, new Rectangle(15, 342, 770, 123), Color.White);
                spriteBatch.DrawString(font, this.name, new Vector2(30, 357), Color.White);
                spriteBatch.DrawString(font, this.saying[0], new Vector2(30, 372), Color.White);
                spriteBatch.DrawString(font, this.saying[1], new Vector2(30, 387), Color.White);
                spriteBatch.DrawString(font, this.saying[2], new Vector2(30, 402), Color.White);
                spriteBatch.DrawString(font, this.saying[3], new Vector2(30, 417), Color.White);
            }
            else if ((this.state & TALK_2) == TALK_2)
            {
                spriteBatch.Draw(this.sprites[1], new Rectangle(this.x, this.y, 256, 256), Color.White);
                spriteBatch.Draw(this.textbox, new Rectangle(15, 342, 770, 123), Color.White);
                spriteBatch.DrawString(font, this.name, new Vector2(30, 357), Color.White);
                spriteBatch.DrawString(font, this.saying[0], new Vector2(30, 372), Color.White);
                spriteBatch.DrawString(font, this.saying[1], new Vector2(30, 387), Color.White);
                spriteBatch.DrawString(font, this.saying[2], new Vector2(30, 402), Color.White);
                spriteBatch.DrawString(font, this.saying[3], new Vector2(30, 417), Color.White);
            }
        }
    }
}
